import 'package:dancers_map/data/bean_dancer_details.dart';
import 'package:dancers_map/data/bean_dancer_md.dart';
import 'package:dancers_map/data/bean_dancers.dart';
import 'package:dancers_map/data/bean_dancers_spider.dart';
import 'package:dancers_map/main.dart';
import 'package:dancers_map/network/enum_connect_state.dart';
import 'package:dancers_map/network/http_client.dart';
import 'package:dancers_map/network/state_request.dart';
import 'package:get/get.dart';
import 'package:dio/dio.dart' as DIO;

class SpiderDancerDetailsCtrl extends GetxController {
  static SpiderDancerDetailsCtrl get to => Get.find();

  final detailsState = ConnectState.none.obs; //刷新的状态
  final detailsLive = DancerDetails.fromParams().obs;

  final mdState = ConnectState.none.obs; //刷新的状态
  final mdLive = DancerMD.fromParams().obs;

  Future<void> queryDancerDetails(String id) async {
    detailsState.value = ConnectState.waiting;
    detailsState.refresh();

    try {
      var formData = DIO.FormData.fromMap({
        'id': id,
      });

      String url = '$BASE_HOST/dancer/spider/details/query';

      print('url:$url');

      //这里为了显示加载过程，可退
      var response = await HttpManager.getInstance().dio!.post(
            url,
            data: formData,
          );

      DancerDetails result = DancerDetails.fromJson(response.data);

      if (result.err == RequestState.SUCCESS) {
        detailsLive.value = result;

        detailsState.value = ConnectState.done;
        detailsState.refresh();
      } else {
        detailsState.value = ConnectState.err;
        detailsState.refresh();
      }
    } catch (e) {
      print(e);
      detailsState.value = ConnectState.err;
      detailsState.refresh();
    }
    print('查询${id}dancerDetails完毕');
  }


  Future<void> queryDancerMD(String id) async {
    mdState.value = ConnectState.waiting;
    mdState.refresh();

    try {
      var formData = DIO.FormData.fromMap({
        'id': id,
      });

      String url = '$BASE_HOST/dancer/spider/details/md/query';

      print('url:$url');

      //这里为了显示加载过程，可退
      var response = await HttpManager.getInstance().dio!.post(
            url,
            data: formData,
          );

      DancerMD result = DancerMD.fromJson(response.data);

      if (result.err == RequestState.SUCCESS) {
        mdLive.value = result;

        mdState.value = ConnectState.done;
        mdState.refresh();
      } else {
        mdState.value = ConnectState.err;
        mdState.refresh();
      }
    } catch (e) {
      print(e);
      mdState.value = ConnectState.err;
      mdState.refresh();
    }
    print('查询${id}dancerMD完毕');
  }
}
